package com.aplink.generic.google.maps.ui;

import com.aplink.generic.GenericApplication;
import com.aplink.generic.google.maps.PolyUtil;
import com.aplink.generic.pojo.googlemaps.DirectionsResult;
import com.aplink.sg.R;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;

public class DirectionUtil {
	private static DirectionUtil sIntance = null;
	public static DirectionUtil getInstance() {
		synchronized (DirectionUtil.class) {
			if (sIntance == null) {
				sIntance = new DirectionUtil();
			}
			return sIntance;
		}
	}
	private Polyline mDirectionLine;
	private Polyline mDirectionOutline;

	private Marker mMarkerDestination;

	private Marker mMarkerStartPoint;

	public void DrawDirectionline(GoogleMap googleMap,
	        DirectionsResult directionsResult) {
		if (mDirectionLine != null) {
			mDirectionLine.remove();
		}
		if (mDirectionOutline != null) {
			mDirectionOutline.remove();
		}
		if (mMarkerDestination != null) {
			mMarkerDestination.remove();
		}

		if (mMarkerStartPoint != null) {
			mMarkerStartPoint.remove();
		}
		mDirectionOutline = googleMap
		        .addPolyline(new PolylineOptions()
		                .width(15f)
		                .zIndex(0)
		                .color(GenericApplication.getInstance()
		                        .getApplicationContext().getResources()
		                        .getColor(android.R.color.holo_red_dark))
		                .addAll(PolyUtil.decode(directionsResult
		                        .getDirectionsRoutes().getOverviewPolyline())));
		mDirectionLine = googleMap.addPolyline(new PolylineOptions()
		        .width(10f)
		        .zIndex(1)
		        .color(GenericApplication.getInstance().getApplicationContext()
		                .getResources()
		                .getColor(android.R.color.holo_blue_dark))
		        .addAll(PolyUtil.decode(directionsResult.getDirectionsRoutes()
		                .getOverviewPolyline())));
		mMarkerDestination = googleMap
		        .addMarker(new MarkerOptions()
		                .anchor(0.5f, 0.5f)
		                .position(
		                        directionsResult.getDirectionsRoutes()
		                                .getDirectionsLeg().getEndLocation())
		                .icon(BitmapDescriptorFactory
		                        .fromResource(R.drawable.ic_directions_form_destination)));

		mMarkerStartPoint = googleMap
		        .addMarker(new MarkerOptions()
		                .anchor(0.5f, 0.5f)
		                .position(
		                        directionsResult.getDirectionsRoutes()
		                                .getDirectionsLeg().getStartLocation())
		                .icon(BitmapDescriptorFactory
		                        .fromResource(R.drawable.ic_directions_form_startpoint)));
	}
}
